package net.cityside.jlearn.performance;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MockBigData {

	public static BigObj[] generateBigObject(int count) {
		System.out.println("-- 创建一堆大对象:" + count);
		BigObj[] bos = new BigObj[count];
		for (int i = 0; i < count; i++) {
			bos[i] = new BigObj();
			System.out.println("---- 已创建对象 No." + i);
		}
		System.out.println(".. 创建一堆大对象完毕");
		return bos;
	}

	public static Thread[] generateManyThreads(int count) {
		System.out.println("-- 创建一堆线程:" + count);
		Thread[] ts = new Thread[count];
		for (int i = 0; i < count; ++i) {
			ts[i] = new Thread(new Runnable() {

				@Override
				public void run() {
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < 1000; i++) {
						sb.append("" + i);
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							return;
						}
					}
				}
			});
			ts[i].start();
			System.out.println("---- Thread No. " + i);
		}
		System.out.println(".. 创建一堆线程完毕");
		return ts;
	}

	public static class BigObj {
		private String name;
		private long[] counts;
		private List<StringBuffer> sbs;

		public BigObj() {
			Random random = new Random();
			name = String.valueOf(random.nextLong());
			counts = new long[1000];
			sbs = new ArrayList<StringBuffer>();

			for (int i = 0; i < counts.length; i++) {
				counts[i] = random.nextLong();
				StringBuffer sb = new StringBuffer(name);
				for (int j = 0; j < counts.length; j++) {
					sb.append(random.nextLong() + "aghkahgkegk");
				}
				sbs.add(sb);
			}

		}
	}
}
